import { defineStore } from 'pinia';

export interface IPieceInfo {
  x: number;
  y: number;
  c: number; // 类型
  fixed: boolean; // 是否可移动
  isArrived?: boolean; // 途经点或终点是否到达
}
export interface IPreset {
  data:Array<any>
}
export interface IMaterial {
  image: string; // 图片路径，相对于学件根目录（OEBPS目录）的路径
}
interface IOperation {
  type: string;
  data: any;
  time: number;
}





export function createStore(pageNumber: number, widgetId: string) {
  return defineStore(`p${pageNumber}_${widgetId}`, {
    state() {
      return {
        preset: {} as IPreset ,
        clickColor:"",
        materials:[] as Array<IMaterial> ,
        stickImg:[] as Array<IMaterial> ,
        background:[] as Array<IMaterial> ,
        fullPath: '',
        initialPreset: [] as Array<IPieceInfo>,
        operations: [] as Array<any>,
        historyIndex: -1,
        submitData:{} as any,
        changNum:0,
      };
    },
    actions: {
      setFullPath(value: string) {
        // 去除掉结尾的/
        this.fullPath = value.replace(/\/+$/, '');
      },
      setSubmitData(data){
        this.submitData = data;
      },
      setChangNum(num){
        this.changNum = num;
      },
      reset() {
        this.preset = JSON.parse(JSON.stringify(this.initialPreset));
        this.operations = [];
      },
      setOperations(op){
        this.operations = op
      },
      setPreset(preset: IPreset) {
        this.preset = preset;
        this.initialPreset = JSON.parse(JSON.stringify(preset));
      },
      setClickColor(value:string){
        this.clickColor = value
      },
      setBack(index:number){
        this.historyIndex = index 
      },
     
    }
  })
}
